//
// Created by ta&to on 2022/8/16.
//
#include "tcp_connect.h"
void init_conn(log_t *logt, int listen_port, int outtime_sec, void* socket_func, void* tcp_func){
    // 处理IO
    tcp_socket_event_t hset;
    tcp_socket_event_handle_t handler;
    hset.logs = logt;
    hset.max_link = MAXCLINE;
    hset.socket_fd = CreateTcpSocket(logt, NULL, listen_port, hset.max_link);
    hset.maxfd = hset.socket_fd;
    handler.data_len = BUF_SIZE;
    handler.func_read = socket_func;
    hset.func_handler = &handler;
    hset.srdc = (struct socket_request_data_container *) tcp_malloc(sizeof(struct socket_request_data_container));
    hset.srdc->req_data = (Leader *) tcp_malloc(sizeof(Leader));
    initLeader(hset.srdc->req_data);
    if(outtime_sec == -1)
        hset.tvp = NULL;
    else {
        struct timeval tvp;
        tvp.tv_sec = outtime_sec;
        tvp.tv_usec = 0;
        hset.tvp = &tvp;
    }
#ifdef TCPSERVER_OPEN
    // 处理tcp
    tcp_handler_t hht;
    tcp_event_context_control_t hecct;
    tcp_rep_pak_t hrpt;
    hht.func = tcp_func;
    hecct.func_handler = &hht;
    hrpt.hset = &hset;
    hrpt.hctx = &hecct;
    hset.hcontxt = &hrpt;
#endif
    // IO模型调用
#if defined(SOCKET_SERVER_SELECT)
    Select(logt, &hset);
#elif defined(SOCKET_SERVER_POLL)
    Poll(logt, &hset);
#elif defined(SOCKET_SERVER_EPOLL)
    Epoll(logt, &hset);
#endif
}
